import BuffUtil from '@/BuffUtil';
import UnitItemEvent from '@/event/UnitItemEvent';
import BaseUtil from '@/BaseUtil';
import ItemUtil from '@/ItemUtil';

import RandomUtil from '@/RandomUtil';
import MovementCommon from 'code/Core/Common/MovementCommon';
import UnitStateUtil from '@/UnitStateUtil';

interface IParams {
    damagePool: unit[];
}

export default class Item_水精灵的祝福 {
    static id = 'C00A';
    static itemId = FourCC('C00A');
    static 名称: string = '水精灵的祝福';
    static 描述: string = '为你提供一个吸收伤害的护盾';
    static 图标: string = 'assets\\item\\INV_7_0Raid_Necklace_12C.blp';
    static buffId: string = 'buff_item_shuijinglingzhufu';
    static buffType: AppBuffType = {
        id: 'buff_item_shuijinglingzhufu',
        name: Item_水精灵的祝福.名称,
        ubertip: Item_水精灵的祝福.描述,
        art: Item_水精灵的祝福.图标,
        dur: 11,
        targetAttach: 'origin',
        targetArt: 'OrbWaterX.mdx',
    };
    static effectBuffId = 'buff_item_shuijingling_ef';
    static effectBuffType: AppBuffType = {
        id: 'buff_item_shuijingling_ef',
        name: Item_水精灵的祝福.名称,
        ubertip: '攻速降低了',
        art: Item_水精灵的祝福.图标,
        attribute: {
            attackSpd_p: 0.5,
        },
        targetArt: 'Abilities\\Spells\\Other\\FrostDamage\\FrostDamage.mdl',
        targetAttach: 'origin',
        dur: 3,
    };
    static onEffectEnd(p: LineMissileConfig) {
        p.other = null;
    }
    static effectInterval(p: LineMissileConfig<IParams>, hitBody: unit) {
        if (!p.other.damagePool.includes(hitBody)) {
            BuffUtil.addBuff(hitBody, Item_水精灵的祝福.effectBuffId, p.caster);
            DamageSystemInstance.applyIceDamage(hitBody, p.caster, 500);
            p.other.damagePool.push(hitBody);
        }
        return true;
    }
    static cooldownPool: Map<unit, number> = new Map();
    constructor() {
        BuffUtil.registerBuffType(Item_水精灵的祝福.buffType);
        BuffUtil.registerBuffType(Item_水精灵的祝福.effectBuffType);
        // 护盾控制部分
        se.onUnitPickupItem(Item_水精灵的祝福.pickupItem);
        BaseUtil.onTimer(1, Item_水精灵的祝福.timerEffect);
        // 攻击特效部分
        EventManager.onTakeAttack(Item_水精灵的祝福.attackEffect);
    }
    static attackEffect(d: DamageEventData) {
        if (!ItemUtil.isUnitHasItem(d.target, Item_水精灵的祝福.itemId)) return;
        let r = RandomUtil.randomPercent();
        if (r > 0.15) return;
        let other: IParams = {
            damagePool: [],
        };
        MovementCommon.LineMissile({
            distance: 450,
            speed: 300,
            radius: 125,
            angle: GetUnitFacing(d.target),
            missile: 'Abilities\\Spells\\Other\\CrushingWave\\CrushingWaveMissile.mdl',
            caster: d.target,
            onHit: Item_水精灵的祝福.effectInterval,
            onFinish: Item_水精灵的祝福.onEffectEnd,
            other,
            is_through: true,
        });
    }
    static timerEffect() {
        for (let [key, value] of Item_水精灵的祝福.cooldownPool) {
            value -= 1;
            if (value <= 0) {
                if (ItemUtil.isUnitHasItem(key, Item_水精灵的祝福.id)) {
                    Item_水精灵的祝福.effect(key);
                    Item_水精灵的祝福.cooldownPool.set(key, 10);
                } else {
                    Item_水精灵的祝福.cooldownPool.delete(key);
                }
            } else {
                Item_水精灵的祝福.cooldownPool.set(key, value);
            }
        }
        return true;
    }
    static pickupItem(d: UnitItemEvent) {
        let u = GetTriggerUnit();
        let item = GetManipulatedItem();
        if (GetItemTypeId(item) != Item_水精灵的祝福.itemId) return;
        if (Item_水精灵的祝福.cooldownPool.has(u)) return;
        Item_水精灵的祝福.effect(u);
    }
    static effect(u: unit) {
        let value = 500 + UnitStateUtil.getMaxLife(u) * 0.1;
        ShieldSystem.添加护盾值(u, Item_水精灵的祝福.buffId, value, false, u);
        BuffUtil.addBuff(u, Item_水精灵的祝福.buffId, u);
        if (!Item_水精灵的祝福.cooldownPool.has(u)) {
            Item_水精灵的祝福.cooldownPool.set(u, 10);
        }
    }
}
